<template>
	<view :data-theme='theme()' :class="theme() || ''">
		<view class="top-tabbar">
			<view :class="state_active == 0 ? 'tab-item active' : 'tab-item'" @click="stateFunc(0)">全部订单</view>
			<view :class="state_active == 1 ? 'tab-item active' : 'tab-item'" @click="stateFunc(1)">待付款</view>
			<view :class="state_active == 2 ? 'tab-item active' : 'tab-item'" @click="stateFunc(2)">待发货</view>
			<view :class="state_active == 3 ? 'tab-item active' : 'tab-item'" @click="stateFunc(3)">待收货</view>
			<!-- <view :class="state_active == 4 ? 'tab-item active' : 'tab-item'" @click="stateFunc(4)">待评价</view> -->
		</view>
		<!--列表-->
		<scroll-view scroll-y="true" class="scroll-Y" :style="'height:' + scrollviewHigh + 'px;'" lower-threshold="50"
			@scrolltolower="scrolltolowerFunc">
			<view :class="topRefresh ? 'top-refresh open' : 'top-refresh'">
				<view class="circle" v-for="(circle, n) in 3" :key="n"></view>
			</view>
			<view class="order-list">
				<view class="item" v-for="(item, index) in listData" :key="index">
					<view class="d-b-c pb38">
						<view class="state">
							<text class="redF6"><text v-if="item.isyufu==1">预付款-</text>{{ item.state_text }}2</text>{{ item.shangmen_time?"（"+item.shangmen_time+"上门）":'' }} 
						</view>
					</view>
					<view class="order-head d-b-c" >
						<view>
							<!-- <text class="state-text">{{ item.order_source_text }}</text> -->
							 <text class="state-text">{{ item.jiedanorderde.priceTypeText }}</text> 
							<text class="shop-name flex-1 fb">订单号：{{ item.order_no }}</text>
						</view>
						
					</view>

					<view class="order-head d-b-c"  style="padding: 1em 0 0 0;" v-if="item.fenleimingc">
						<view> 
							<text class="shop-name flex-1 fb">分类：{{ item.fenleimingc}}</text>
						</view>
						
					</view>
					<view class="order-head d-b-c" style="padding: 1em 0 0 0;" v-if="item.jiedanorderde.kaishidizhi">
						<view>
							<text class="shop-name flex-1 fb">起始地：{{ item.jiedanorderde.kaishidizhi}}</text>
						</view>
						
					</view>
					<view class="order-head d-b-c"  style="padding: 1em 0 0 0;" v-if="item.jiedanorderde.jishudizhi">
						<view> 
							<text class="shop-name flex-1 fb">目的地：{{ item.jiedanorderde.jishudizhi}}</text>
						</view>
						
					</view>



				

					<view class="order-head d-b-c"  style="padding: 1em 0 0 0;"v-if="item.shuju_fahuodianhua">
						<view> 
							<text class="shop-name flex-1 fb">发货人：{{ item.shuju_fahuoren}}-{{ item.shuju_fahuodianhua}}</text>
						</view>
						
					</view>
					<view class="order-head d-b-c"  style="padding: 1em 0 0 0;"v-if="item.shuju_shouhurendianhua">
						<view> 
							<text class="shop-name flex-1 fb">收货人：{{ item.shuju_shouhuren}}-{{ item.shuju_shouhurendianhua}}</text>
						</view>
						
					</view>
					<view class="order-head d-b-c"  style="padding: 1em 0 0 0;" v-show="item.package_method >0">
						<view> 
							<text class="shop-name flex-1 fb">包装方式：{{item.package_method == 0 ? '' : item.package_method == 1 ? '缠膜包装' : item.package_method == 2 ? '电动木架' : item.package_method == 3 ? '摩托木架' : item.package_method == 4 ? '木箱铁架' : ''}}</text>
						</view>
						<text v-show="item.package_method > 0" style="color: red;">
							{{ item.package_method == 1 ? (getPacketPt + '元/立方线下额外收取') : item.package_method == 2 ? (getPacketZy + '元/立方线下额外收取') : item.package_method == 3 ? (getPacketHh + '元/立方线下额外收取') : item.package_method == 4 ? (getPacketMt + '元/立方线下额外收取') : '' }}
						</text>
					</view>
					<view class="order-head d-b-c"  style="padding: 1em 0 0 0;" >
						<view> 
							<text class="shop-name flex-1 fb">收货方式：{{ item.delivery_type.text}}</text>
						</view>
					</view>

					<view class="order-head d-b-c"  style="padding: 1em 0 0 0;" v-if="item.isBaojia==1">
						<view> 
							<text class="shop-name flex-1 fb">已保价：<text style="color: chocolate;">{{ item.baojiaMoney}}元</text></text>
						</view>
						
					</view>
					<view class="order-head d-b-c"  style="padding: 1em 0 0 0;" v-if="item.isBaojia==1">
						<view> 
							<text class="shop-name flex-1 fb"> 保价费：<text style="color: chocolate;">{{item.baojiaMoneys}}元</text></text>
						</view>
						
					</view>
					<view class="order-head d-b-c"  style="padding: 1em 0 0 0;" v-if="item.isyufu==1">
						<view> 
							<text class="shop-name flex-1 fb">定金：<text style="color: chocolate;">{{ item.isyufumoney}}元</text></text>
						</view>
						
					</view>
					<view class="one-product d-s-c">
						<view class="cover" v-for="(img, num) in item.product" :key="num">
							<image :src="img.image.file_path" mode="aspectFit"></image>
						</view>
						<view class="pro-info flex-1">{{ item.product[0].product_name }}</view>
						<view class="total-count">
							<view class="left-shadow"></view>
							
							<view class="price f24">
								¥<text class="f32">{{ item.pay_price}}</text>
							</view>
							<view class="count">共{{ JSON.stringify(item.taishu)}}台</view>
							
							
						</view>
					</view>
					
					<view class="order-bts">
						<block v-if="item.order_status.value ==10">
							<!-- 未支付取消订单 -->
							<button @click="cancelOrder(item.order_id)" class="theme-borderbtn"
								v-if="item.pay_status.value == 10">取消</button>
							<!-- 已支付取消订单 -->
							<block v-if="item.pay_status.value == 20 && item.delivery_status.value == 10">
								<button @click="cancelOrder(item.order_id)" class="theme-borderbtn">申请取消</button>
							</block>
							<!-- 订单核销码 -->
							<template
								v-if="item.pay_status.value == 20 && item.delivery_type.value == 20 && item.delivery_status.value == 10">
								<button v-if="item.order_source == 30 && item.assemble_status==20"
									@click="onQRCode(item.order_id)" class="theme-borderbtn">核销码</button>
								<button v-if="item.order_source != 30" @click="onQRCode(item.order_id)"
									class="theme-borderbtn">核销码</button>
							</template>
							<!-- 订单付款 -->
							<!-- <block v-if="item.pay_status.value == 10"><button class="theme-btn"
									@click="onPayOrder(item.order_id)">付款</button></block> -->
							<!-- 确认收货 -->
							<block v-if="item.delivery_status.value == 20 && item.receipt_status.value == 10">
								<button class="btn-red" style="background: #FF6B6B;" 
									 @click="showTrackInfo(item)">查看物流
								</button>
								<button class="theme-btn" v-if="item.pay_type.value ==20"
									@click="wxOrder(item)">确认收货</button>
								<button class="theme-btn" v-else @click="orderReceipt(item.order_id)">确认收货</button>
							</block>
						</block>
						<text v-if="item.order_status.value == 21" class="count">取消申请中</text>
						<!-- 订单评价 -->
						<!-- <button class="theme-btn" v-if="item.order_status.value == 30 && item.is_comment == 0"
							@click="gotoEvaluate(item.order_id)">评价</button> -->
						
					</view>
				</view>
				<view class="d-c-c p30" v-if="listData.length == 0 && !loading">
					<text class="iconfont icon-wushuju"></text>
					<text class="cont">亲，暂无相关记录哦</text>
				</view>
				<uni-load-more v-else :loadingType="loadingType"></uni-load-more>
			</view>
		</scroll-view>
		
		<!-- 轨迹信息弹窗 -->
		<view class="track-popup" v-show="showTrackPopup" @click="hideTrackPopup">
			<view class="track-content" @click.stop>
				<view class="track-header">
					<text class="track-title">物流轨迹</text>
					<text class="track-close" @click="hideTrackPopup">×</text>
				</view>
				<view class="track-body">
					<!-- 路线图 -->
					<view class="track-timeline" v-if="trackList.length > 0">
						<view class="timeline-item" v-for="(track, index) in trackList" :key="index">
							<view class="timeline-car" :class="{ 'active': index === 0 }">
							<view class="car-body"></view>
							<view class="car-wheel car-wheel-left"></view>
							<view class="car-wheel car-wheel-right"></view>
						</view>
							<view class="timeline-line" v-if="index < trackList.length - 1"></view>
							<view class="timeline-content">
								<view class="track-time">{{ formatTime(track.create_time) }}</view>
								<view class="track-desc">{{ track.guijiForm }}</view>
							</view>
						</view>
					</view>
					<view class="track-empty" v-if="trackList.length === 0">
						<text>暂无轨迹信息</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import uniLoadMore from '@/components/uni-load-more.vue';
	import {
		pay
	} from '@/common/pay.js';
	import { mapGetters } from 'vuex'
	export default {
		components: {
			uniLoadMore
		},
		data() {
			return {
				/*手机高度*/
				phoneHeight: 0,
				/*可滚动视图区域高度*/
				scrollviewHigh: 0,
				/*状态选中*/
				state_active: 0,
				/*顶部刷新*/
				topRefresh: false,
				/*数据*/
				listData: [],
				/*数据类别*/
				dataType: 'all',
				/*是否显示支付类别弹窗*/
				isPayPopup: false,
				/*支付方式*/
				pay_type: 20,
				/*订单id*/
				order_id: 0,
				/*最后一页码数*/
				last_page: 0,
				/*当前页面*/
				page: 1,
				/*每页条数*/
				list_rows: 10,
				/*有没有等多*/
				no_more: false,
				/*是否正在加载*/
				loading: true,
				/*是否显示核销二维码*/
				isCodeImg: false,
				codeImg: '',
				/*是否显示支付宝支付，只有在h5，非微信内打开才显示*/
				showAlipay: false,
				isfirst: false,
				mch_id: "",
				/* 轨迹信息弹窗 */
				showTrackPopup: false,
				trackList:[]
			};
		},
		computed: {
			/*加载中状态*/
			loadingType() {
				if (this.loading) {
					return 1;
				} else {
					if (this.listData.length != 0 && this.no_more) {
						return 2;
					} else {
						return 0;
					}
				}
			},
			...mapGetters(['getPacketPt', 'getPacketZy', 'getPacketMt', 'getPacketHh'])
		},
		onLoad(e) {
			if (typeof e.dataType != 'undefined') {
				this.dataType = e.dataType;
			}

			if (this.dataType == 'payment') {
				this.state_active = 1;
			} else if (this.dataType == 'received') {
				this.state_active = 3;
			} else if (this.dataType == 'comment') {
				this.state_active = 4;
			} else if (this.dataType == 'delivery') {
				this.state_active = 2;
			}
		},
		mounted() {
			this.init();
			this.initData();
			/*获取订单列表*/
			this.getData();
		},
		onShow() {
			if (this.isfirst) {
				this.initData();
				this.getData();
			}
		},
		methods: {
			formatTime(timestamp) {
				if (!timestamp) return '';
				
				// 如果是字符串格式的时间，直接返回
				if (typeof timestamp === 'string' && timestamp.includes('-')) {
					return timestamp;
				}
				
				// 如果是时间戳，转换为时间格式
				let date = new Date(parseInt(timestamp) * 1000);
				let year = date.getFullYear();
				let month = (date.getMonth() + 1).toString().padStart(2, '0');
				let day = date.getDate().toString().padStart(2, '0');
				let hours = date.getHours().toString().padStart(2, '0');
				let minutes = date.getMinutes().toString().padStart(2, '0');
				let seconds = date.getSeconds().toString().padStart(2, '0');
				
				return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
			},
			/* 隐藏轨迹信息弹窗 */
			hideTrackPopup() {
				this.showTrackPopup = false;
				this.trackList = [];
			},
			/* 显示轨迹信息 */
			showTrackInfo(item) {
				let self = this;
				uni.showLoading({
					title: '加载中'
				});
				self._get(
					'user.order/track', {
						order_id: item.order_id
					},
					function(res) {
						uni.hideLoading();
						if (res.code === 1) {
							self.trackList = res.data.list || [];
							self.showTrackPopup = true;
						} else {
							uni.showToast({
								title: res.msg || '获取轨迹信息失败',
								icon: 'none'
							});
						}
					},
					function(err) {
						uni.hideLoading();
						uni.showToast({
							title: '网络错误，请重试',
							icon: 'none'
						});
					}
				);
			},
			initData() {
				let self = this;
				self.page = 1;
				self.listData = [];
				self.no_more = false;
			},
			cancelAdvance(e) {
				let self = this;
				let order_id = e;
				uni.showModal({
					title: '提示',
					content: '您确定要取消吗?',
					success: function(o) {
						if (o.confirm) {
							uni.showLoading({
								title: '正在处理'
							});
							self._get(
								'plus.advance.Order/cancelFront', {
									order_id: order_id
								},
								function(res) {
									uni.hideLoading();
									uni.showToast({
										title: '操作成功',
										duration: 2000,
										icon: 'success'
									});
									self.listData = [];
									self.getData();
								}
							);
						}
					}
				});
			},
			depositPay(e) {
				this.gotoPage('/pages/order/confirm-order?order_type=retainage&order_id=' + e)
			},
			/*初始化*/
			init() {
				let self = this;
				uni.getSystemInfo({
					success(res) {
						self.phoneHeight = res.windowHeight;
						// 计算组件的高度
						let view = uni.createSelectorQuery().in(self).select('.top-tabbar');
						view.boundingClientRect(data => {
							let h = self.phoneHeight - data.height;
							self.scrollviewHigh = h;
						}).exec();
					}
				});
			},
			/*状态切换*/
			stateFunc(e) {
				let self = this;
				if (self.state_active != e) {
					self.page = 1;
					self.loading = true;
					self.state_active = e;
					switch (e) {
						case 0:
							self.listData = [];
							self.dataType = 'all';
							break;
						case 1:
							self.listData = [];
							self.dataType = 'payment';
							break;
						case 2:
							self.listData = [];
							self.dataType = 'delivery';
							break;
						case 3:
							self.listData = [];
							self.dataType = 'received';
							break;
						case 4:
							self.listData = [];
							self.dataType = 'comment';
							break;
					}
					self.getData();
				}
			},

			/*可滚动视图区域到底触发*/
			scrolltolowerFunc() {
				let self = this;
				if (self.no_more) {
					return;
				}
				self.page++;
				if (self.page <= self.last_page) {
					self.getData();
				} else {
					self.no_more = true;
				}
			},

			/*获取数据*/
			getData() {
				let self = this;
				self.loading = true;
				let dataType = self.dataType;
				self._get(
					'user.order/lists', {
						dataType: dataType,
						page: self.page,
						pay_source: self.getPlatform(),
						list_rows: self.list_rows
					},
					function(res) {
						self.loading = false;
						self.listData = self.listData.concat(res.data.list.data);
						self.last_page = res.data.list.last_page;
						self.mch_id = res.data.mch_id;
						if (res.data.list.last_page <= 1) {
							self.no_more = true;
						} else {
							self.no_more = false;
						}
						if (res.data.show_alipay) {
							self.showAlipay = true;
						}
						self.isfirst = true;
					}
				);
			},

			/*跳转页面*/
			gotoOrder(e) {
				this.gotoPage('/pages/order/order-detail?order_id=' + e);
			},

			/*隐藏支付方式*/
			hidePopupFunc() {
				this.isPayPopup = false;
			},
			toShop(id) {
				this.gotoPage('/pages/shop/shop?shop_supplier_id=' + id);
			},
			/*去支付*/
			payTypeFunc(payType) {
				let self = this;
				self.isPayPopup = false;
				uni.showLoading({
					title: '加载中'
				});
				self._post(
					'user.order/pay', {
						payType: payType,
						order_id: self.order_id,
						pay_source: self.getPlatform()
					},
					function(res) {
						pay(res, self);
					}
				);
			},

			/*支付方式选择*/
			onPayOrder(orderId) {
				let self = this;
				self.gotoPage('/pages/order/cashier?order_type=1&order_id=' + orderId);
			},
			wxOrder(item) {
				let self = this;
				
				
				self._post(
					'user.order/receipt', {
						order_id: item.order_id
					},
					function(res) {
						uni.showToast({
							title: res.msg,
							duration: 2000,
							icon: 'success'
						});
						self.listData = [];
						self.getData();
					}
				);
				
				return;
				
				if (wx.openBusinessView) {
					wx.openBusinessView({
						businessType: 'weappOrderConfirm',
						extraData: {
							merchant_id: self.mch_id,
							merchant_trade_no: item.trade_no,
							transaction_id: item.transaction_id
						},
						success() {
							self._post(
								'user.order/receipt', {
									order_id: item.order_id
								},
								function(res) {
									uni.showToast({
										title: res.msg,
										duration: 2000,
										icon: 'success'
									});
									self.listData = [];
									self.getData();
								}
							);
						},
						fail() {
							//dosomething
						},
						complete() {
							//dosomething
						}
					});
				} else {
					//引导用户升级微信版本
				}
			},
			/*确认收货*/
			orderReceipt(order_id) {
				let self = this;
				uni.showModal({
					title: '提示',
					content: '您确定要收货吗?',
					success: function(o) {
						if (o.confirm) {
							uni.showLoading({
								title: '正在处理'
							});
							self._post(
								'user.order/receipt', {
									order_id: order_id
								},
								function(res) {
									uni.hideLoading();
									uni.showToast({
										title: res.msg,
										duration: 2000,
										icon: 'success'
									});
									self.listData = [];
									self.getData();
								}
							);
						} else {
							uni.showToast({
								title: '取消收货',
								duration: 1000,
								icon: 'none'
							});
						}
					}
				});
			},
			/*计算时分秒*/
			countDown(t) {
				let time = t - (Date.now() / 1000);
				//天数
				let day = Math.floor(time / (60 * 60 * 24));
				//取模（余数）
				let modulo = time % (60 * 60 * 24);
				//小时数
				let hour = Math.floor(modulo / (60 * 60));
				modulo = modulo % (60 * 60);
				//分钟
				let minute = Math.floor(modulo / 60);
				//秒
				let second = modulo % 60;
				day = this.convertTwo(day);
				hour = this.convertTwo(hour);
				minute = this.convertTwo(minute);
				second = this.convertTwo(second);
				let text = parseInt(day) > 0 ? (parseInt(day) + '天 ') : '' + parseInt(hour) + '时' + parseInt(minute) +
					'分' + parseInt(second) + '秒';
				return text
			},
			/*取消订单*/
			cancelOrder(e) {
				let self = this;
				let order_id = e;
				uni.showModal({
					title: '提示',
					content: '您确定要取消吗?',
					success: function(o) {
						if (o.confirm) {
							uni.showLoading({
								title: '正在处理'
							});
							self._get(
								'user.order/cancel', {
									order_id: order_id
								},
								function(res) {
									uni.hideLoading();
									uni.showToast({
										title: '操作成功',
										duration: 2000,
										icon: 'success'
									});
									self.listData = [];
									self.getData();
								}
							);
						}
					}
				});
			},

			/*去评论*/
			gotoEvaluate(e) {
				this.gotoPage('/pages/order/evaluate/evaluate?order_id=' + e);
			},

			/*核销码*/
			onQRCode(e) {
				let self = this;
				uni.showLoading({
					title: '加载中'
				});
				let order_id = e;
				let source = self.getPlatform();
				self._get(
					'user.order/qrcode', {
						order_id: order_id,
						source: source
					},
					function(res) {
						uni.hideLoading();
						self.isCodeImg = true;
						self.codeImg = res.data.qrcode;
					}
				);
			},
			nowOverTime(t) {

				let now = new Date().getTime();
				let time = new Date(t * 1000).getTime();
				return now >= time
			},
			/*关闭核销二维码*/
			hideCodePopupFunc() {
				this.isCodeImg = false;
			},

			/*分享拼团*/
			gotoAssembleShare(e) {
				this.gotoPage('/pages/plus/assemble/fight-group-detail/fight-group-detail?assemble_bill_id=' + e);
			}
		}
	};
</script>

<style lang="scss">
	@import url("css/myorder.css");
.chakanguiji{float: right;font-size: 0.7rem;color: #fff;background-color: chocolate;border-radius: 0.3rem;padding: 0.2rem 0.5rem;}
	
	/* 轨迹弹窗样式 */
	.track-popup {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.5);
		z-index: 9999;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.track-content {
		width: 80%;
		max-height: 70%;
		background-color: #fff;
		border-radius: 10px;
		overflow: hidden;
	}
	
	.track-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 5px 10px;
		border-bottom: 1px solid #eee;
		background-color: #f8f8f8;
	}
	
	.track-title {
		font-size: 16px;
		font-weight: bold;
		color: #333;
	}
	
	.track-close {
		font-size: 20px;
		color: #999;
		cursor: pointer;
		padding: 5px;
	}
	
	.track-body {
		padding: 20px;
		max-height: 400px;
		overflow-y: auto;
	}
	
	.track-item {
		margin-bottom: 15px;
		padding-bottom: 15px;
		border-bottom: 1px solid #f0f0f0;
	}
	
	.track-item:last-child {
		border-bottom: none;
		margin-bottom: 0;
		padding-bottom: 0;
	}
	
	.track-time {
		font-size: 12px;
		color: #999;
		margin-bottom: 5px;
	}
	
	.track-desc {
		font-size: 14px;
		color: #333;
		line-height: 1.5;
	}
	
	.track-empty {
		text-align: center;
		padding: 40px 0;
		color: #999;
		font-size: 14px;
	}
	
	/* 路线图样式 */
	.track-timeline {
		position: relative;
		padding: 20px 0;
	}
	
	.timeline-item {
		position: relative;
		display: flex;
		align-items: flex-start;
		margin-bottom: 20px;
	}
	
	.timeline-item:last-child {
		margin-bottom: 0;
	}
	
	.timeline-car {
		width: 20px;
		height: 12px;
		flex-shrink: 0;
		margin-top: 5px;
		position: relative;
		z-index: 2;
	}
	
	.car-body {
		width: 100%;
		height: 100%;
		background-color: #ddd;
		border-radius: 6px 6px 2px 2px;
		position: relative;
		border: 2px solid #fff;
		box-shadow: 0 0 0 2px #ddd;
	}
	
	.car-body::before {
		content: '';
		position: absolute;
		top: 2px;
		left: 3px;
		width: 8px;
		height: 4px;
		background-color: #fff;
		border-radius: 1px;
	}
	
	.car-wheel {
		position: absolute;
		width: 4px;
		height: 4px;
		background-color: #333;
		border-radius: 50%;
		bottom: -2px;
	}
	
	.car-wheel-left {
		left: 2px;
	}
	
	.car-wheel-right {
		right: 2px;
	}
	
	.timeline-car.active .car-body {
		background-color: #007aff;
		box-shadow: 0 0 0 2px #007aff;
	}
	
	.timeline-car.active .car-wheel {
		background-color: #fff;
	}
	
	.timeline-line {
		position: absolute;
		left: 9px;
		top: 17px;
		width: 2px;
		height: calc(100% + 3px);
		background-color: #ddd;
		z-index: 1;
	}
	
	.timeline-content {
		flex: 1;
		margin-left: 20px;
		padding: 10px 15px;
		background-color: #f8f9fa;
		border-radius: 8px;
		border-left: 3px solid #007aff;
	}
	
	.timeline-content .track-time {
		font-size: 12px;
		color: #666;
		margin-bottom: 5px;
	}
	
	.timeline-content .track-desc {
		font-size: 14px;
		color: #333;
		line-height: 1.4;
	}
</style>